/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.gnocco.grafics;

import it.gnocco.grafo.Grafo;
import it.gnocco.log.Logger;

import java.awt.*;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * 
 * @author gnocco
 */
public class Finestrella extends JFrame {
	public static Color colore = Color.black;
	public static Color colore_nodo = Color.yellow;
	public static String titolo = "Multi Robot Exploration";
	public ArrayList<Finestrella.JNodo> nodi = new ArrayList<Finestrella.JNodo>();
	public ArrayList<Finestrella.JSpigolo> spigoli = new ArrayList<Finestrella.JSpigolo>();
	private static final String CLASS = "Finestrella";

	public Finestrella(Grafo g) {
		this.setTitle(titolo);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setBackground(this.colore);
		this.setEnabled(true);
		this.setResizable(false);
		this.setSize(700, 400);
		this.setLocation(100, 50);
		this.validate();
		this.repaint();
		int s = g.getSizeNodi();
		File file = new File("coordinate.txt");
		FileReader fr;
		try {
			fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			
			String linea = "";
			int i = 0;
			while((linea=br.readLine())!=null){
				int x = Integer.valueOf(linea.split(";")[0]);
				int y = Integer.valueOf(linea.split(";")[1]);
				this.aggiungiNodo(g.getNodo(i).colore, x , y, 10);
				i++;
			}
			br.close();
			fr.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Logger.log(Logger.error, CLASS, CLASS, "ho trovato un errore nel file di coordinate:");
			Logger.log(Logger.error, CLASS, CLASS, e.getLocalizedMessage());
			for (int i = 0; i < s; i++) {
				int x = (int) (Math.random() * 500) + 10;
				int y = (int) (Math.random() * 300) + 10;
				this.aggiungiNodo(g.getNodo(i).colore, x , y, 10);
			}

		}
		for (int i = 0; i < s; i++) {
			for (int j = 0; j < g.getNodo(i).sizeVicini(); j++) {
				this.aggiungiSpigoli(Color.white, i, g.getNodo(i).getNodo(j).getID());
			}
		}
		this.aggiorna();
	}

	public Finestrella() {
		this.setTitle(titolo);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setBackground(this.colore);
		this.setEnabled(true);
		this.setResizable(false);
		this.setSize(700, 400);
		this.setLocation(100, 0);
		this.validate();
		this.repaint();
	}

	public void aggiorna() {
		this.validate();
		this.repaint();
	}

	public void cambiaTitolo(String title) {
		titolo = title;
		this.setTitle(title);
		aggiorna();
	}

	public void cambiaColore(Color color) {
		colore = color;
		this.setBackground(color);
		aggiorna();
	}

	public void aggiornaNodo(int n, Color color){
		this.nodi.get(n).colore = color;
		this.remove(nodi.get(n));
		this.add(nodi.get(n));
		this.aggiorna();
	}
	
	public void aggiungiNodo(Color color, int x, int y, int raggio) {
		JNodo nodo = new JNodo();
		nodo.colore = color;
		nodo.x_pos = x;
		nodo.y_pos = y;
		nodo.radius = raggio;
		nodi.add(nodo);
		this.add(nodo);
		this.aggiorna();
	}

	public void aggiungiSpigoli(Color color, int X, int Y) {
		JSpigolo spigolo = new JSpigolo();
		spigolo.colore = color;
		spigolo.x1 = nodi.get(X).x_pos;
		spigolo.y1 = nodi.get(X).y_pos;
		spigolo.x2 = nodi.get(Y).x_pos;
		spigolo.y2 = nodi.get(Y).y_pos;
		this.add(spigolo);
		this.aggiorna();

	}

	public class JNodo extends JPanel {
		public Color colore = Color.white;
		public int x_pos = 50;
		public int y_pos = 170;
		public int radius = 10;

		public void paint(Graphics g) {
			g.setColor(colore);
			g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
		}

	}

	public class JSpigolo extends JPanel {
		public Color colore = Color.white;
		public int x1 = 50;
		public int y1 = 170;
		public int x2 = 50;
		public int y2 = 170;

		public void paint(Graphics g) {
			g.setColor(colore);
			g.drawLine(x1, y1, x2, y2);

		}

	}

}
